import type { ApiModel } from '@g/types/global'
import { ikHttp, textHeader } from '@g/api/request'

export const getDependency = (): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/project/dependency')
}
export const addProject = (params: any): Promise<ApiModel> => {
  return ikHttp.post('/server/scaffold/project', params)
}
export const editProject = (params: any): Promise<ApiModel> => {
  return ikHttp.put('/server/scaffold/project', params)
}
export const addTableDefinition = (id: any): Promise<ApiModel> => {
  return ikHttp.get(
    `/server/scaffold/module/table/definition?datasourceId=${id}`
  )
}
export const allDatasource = (): Promise<ApiModel> => {
  return ikHttp.post('/server/datasource/list/all')
}
export const getfieldTypeList = (): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/module/field/type')
}
export const getfieldList = (params: any): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/module/table/field/definition', params)
}
export const addFunSave = (params: any): Promise<ApiModel> => {
  return ikHttp.post('/server/scaffold/module', params)
}
export const editFunSave = (params: any): Promise<ApiModel> => {
  return ikHttp.put('/server/scaffold/module', params)
}
export const getProject = (params: any): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/project/page', params)
}
export const delProject = (id: any): Promise<ApiModel> => {
  return ikHttp.del(`/server/scaffold/project?projectId=${id}`)
}
export const getFun = (params: any): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/module/page', params)
}
export const getForm = (params: any): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/form/page', params)
}
export const addForm = (params: any): Promise<ApiModel> => {
  return ikHttp.post('/server/scaffold/form', params)
}
export const editForm = (params: any): Promise<ApiModel> => {
  return ikHttp.put('/server/scaffold/form', params)
}
export const addList = (params: any): Promise<ApiModel> => {
  return ikHttp.post('/server/scaffold/list', params)
}
export const editList = (params: any): Promise<ApiModel> => {
  return ikHttp.put('/server/scaffold/list', params)
}
export const projectDownload = (params: any): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/project/download', params, {
    responseType: 'blob'
  })
}
export const funDownload = (params: any): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/module/download', params, {
    responseType: 'blob'
  })
}
export const syncFun = (params: any): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/module/sync', params)
}
export const deleteFun = (id: any): Promise<ApiModel> => {
  return ikHttp.del(`/server/scaffold/module?moduleId=${id}`)
}
export const deleteForm = (id: any): Promise<ApiModel> => {
  return ikHttp.del(`/server/scaffold/form?formId=${id}`)
}
export const deleteList = (id: any): Promise<ApiModel> => {
  return ikHttp.del(`/server/scaffold/list?listId=${id}`)
}
export const getType = (): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/type')
}
export const getProjectById = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/project/get?projectId=${id}`)
}
export const getFunById = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/module/get?moduleId=${id}`)
}
export const getFormById = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/form/get?formId=${id}`)
}
export const getListById = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/list/get?listId=${id}`)
}
export const getProjectPreview = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/project/preview?projectId=${id}`)
}
export const getModulePreview = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/module/preview?moduleId=${id}`)
}
export const getModuleField = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/module/table/field/all?moduleId=${id}`)
}
export const getInterfaces = (id: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/form/interfaces?moduleId=${id}`)
}
export const getJson = (link: any): Promise<ApiModel> => {
  return ikHttp.get(`/server/scaffold/form/menuLink?link=${link}`)
}
export const getFieldDefault = (): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/module/table/field/default')
}
export const getJavaType = (tableFieldType: string): Promise<ApiModel> => {
  return ikHttp.get(
    `/server/scaffold/module/table/field/type?tableFieldType=${tableFieldType}`
  )
}
export const getFieldIgnore = (): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/module/table/field/length/ignore')
}
export const getSqlPreview = (params: any): Promise<ApiModel> => {
  return ikHttp.post('/server/scaffold/module/preview', params)
}
export const getFormId = (): Promise<ApiModel> => {
  return ikHttp.get('/server/scaffold/form/id')
}

/** 页面管理 */
export const pageManage = {
  // 新增
  add: (params: any): Promise<ApiModel> => ikHttp.post('/server/scaffold/page/add', params),
  // 删除
  del: (id: string): Promise<ApiModel> => ikHttp.post('/server/scaffold/page/delete', id, textHeader),
  // 修改
  update: (params: any): Promise<ApiModel> => ikHttp.post('/server/scaffold/page/update', params),
  // 查询
  select: (params: any): Promise<ApiModel> => ikHttp.post('/server/scaffold/page/list/page', params),

  // 发布
  publish: (params: any): Promise<ApiModel> => ikHttp.post('/server/scaffold/page/publish', params),
  // 取消发布
  cancelPublish: (id: string): Promise<ApiModel> => ikHttp.post('/server/scaffold/page/publish/cancel', id, textHeader),
  // 详情
  details: (id: string): Promise<ApiModel> => ikHttp.post('/server/application/page/detail', id, textHeader),
  // 模板列表
  listTemplate: (): Promise<ApiModel> => ikHttp.post('/server/application/page-template/list/all'),
  // 生成代码
  codeGen: (params: any): Promise<ApiModel> => ikHttp.post('/scaffold/page/code/gen', params, {
    responseType: 'blob'
  })

}

/** 模型管理 */
export const modelManage = {
  // 新增
  add: (params: any): Promise<ApiModel> => ikHttp.post('/application/model/add', params),
  // 删除
  del: (id: string): Promise<ApiModel> => ikHttp.post('/application/model/delete', id, textHeader),
  // 修改
  update: (params: any): Promise<ApiModel> => ikHttp.post('/application/model/update', params),
  // 查询
  select: (params: any): Promise<ApiModel> => ikHttp.post('/application/model/list/page', params),
  // 全量查询
  selectAll: (showSlaveModelField?: boolean): Promise<ApiModel> => ikHttp.post(`/application/model/list/all?showSlaveModelField=${showSlaveModelField ?? ''}`),
  // 生成代码
  genCode: (params: any): Promise<ApiModel> => ikHttp.post('/application/model/code/gen', params, {
    responseType: 'blob'
  }),
  // 预览建表 sql
  sqlPreview: (params: any): Promise<ApiModel> => ikHttp.post('/application/model/create-table-sql/preview', params),
  // 全量查询数据源
  listAllDatasource: (): Promise<ApiModel> => ikHttp.post('/datasource/list/all'),
  // 查询数据源下对应的表格数据
  listByDatasourceId: (id: string): Promise<ApiModel> => ikHttp.post('/application/model/list/datasource-id', id, textHeader),
  // 模型发布
  publish: (id: string): Promise<ApiModel> => ikHttp.post('/application/model/publish', id, textHeader),
  // 取消模型发布
  publishCancel: (id: string): Promise<ApiModel> => ikHttp.post('/application/model/publish/cancel', id, textHeader)
}
